package com.evaluationSystem.config;

import lombok.Data;

import java.io.Serializable;


/**
 * 响应信息主体
 *
 * @param <T>
 * @author lengleng
 */
@Data
public class R<T> implements Serializable {

    private int code;

    private boolean success = true;

    private String message;

    private T result;

    private R() {
    }

    public static <T> R<T> of(RCode code, boolean success, String message, T data) {
        R<T> r = new R<>();
        r.setCode(code.getCode());
        r.setSuccess(success);
        r.setMessage(code.getMessage());
        r.setResult(data);
        if (message != null && message.length() > 0) {
            r.setMessage(message);
        }
        return r;
    }


    public static <T> R<T> success() {
        return of(RCode.SUCCESS, true, null, null);
    }

    public static <T> R<T> success(T data) {
        return of(RCode.SUCCESS, true, null, data);
    }

    public static <T> R<T> success(String message, T data) {
        return of(RCode.SUCCESS, true, message, data);
    }

    public static <T> R<T> failed() {
        return of(RCode.SERVICE_ERROR, false, null, null);
    }

    public static <T> R<T> failed(RCode rCode) {
        return of(rCode, false, rCode.getMessage(), null);
    }

    public static <T> R<T> failed(String message) {
        return of(RCode.SERVICE_ERROR, false, message, null);
    }

    public static <T> R<T> failed(RCode rCode, String message) {
        return of(rCode, false, message, null);
    }

    public static <T> R<T> failed(RCode rCode, String message, T data) {
        return of(rCode, false, message, data);
    }
}

